﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using CustomsSystem.BLL;
using Maticsoft.Common;

namespace WEB.T_bill
{
    public partial class actualReceivables : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {    
                if (Session["EmpNo"] != null && Session["UserName"] != null)
                    lblinputEmpNo.Text = Session["UserName"].ToString();
                bind();

            }
            tbpayDate.Attributes.Add("onClick", "new WdatePicker()");

            if (ddlaudit .SelectedIndex  == 0)
            {
                Response.Write("<script>alert('该单尚未审核！');window.history.go(-2);</script>");

            }

         //   btnSave.Attributes.Add("onclick", "javascript:return confirm('你确定要保存吗？')");
            
        }

        public void bind()
        {
            
            lblcID.Text = Session["CustomerName"].ToString();
            tbsheetID.Text = Session["sheetID"].ToString();
            int sheetID = int.Parse(tbsheetID.Text.Trim());
            int CID;
            DataSet dsPay = new PayManage().GetInfofromPay(sheetID, 1);
            DataSet dsBill = new CustomsSystem.BLL.BillManage().GetList(sheetID, 1);
            DataTable dt = dsBill.Tables[0];
            dt.Columns.Add("payDate");
            if (1 > dsPay.Tables[0].Rows.Count && dsBill.Tables[0].Rows.Count < 1)
            {
                Response.Write("<script>alert('该单尚未审核！');window.history.go(-1);</script>");
            }
            else
            {
                CID = int.Parse(dsBill.Tables[0].Rows[0][2].ToString());
                string a = dsBill.Tables[0].Rows[0][9].ToString();
                if (a == null || a == "")
                {
                    Response.Write("<script>alert('该单尚未审核！');window.history.go(-1);</script>");
                }
                else
                {
                    ddlaudit.DataSource = GetEmpNum();
                    ddlaudit.DataTextField = "UserName";
                    ddlaudit.DataValueField = "EMPNum";
                    ddlaudit.DataBind();
                    ddlaudit.SelectedValue = a;
                }
                ddlempID.DataSource = GetEmpNum();
                ddlempID.DataTextField = "UserName";
                ddlempID.DataValueField = "EMPNum";
                ddlempID.DataBind();
                
                 if (dsBill.Tables[0].Rows.Count >dsPay.Tables[0].Rows.Count)
                {
                   
                    gvActualReceivables.DataSource = dt;
                    gvActualReceivables.DataBind();
                }
                 else
                 {
                     CID = int.Parse(dsPay.Tables[0].Rows[0][2].ToString());
                     gvActualReceivables.DataSource = dsPay;
                     gvActualReceivables.DataBind();
                 
                 }

            }
 
        }


        /// <summary>
        /// 行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvActualReceivables_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TextBox tbpayDate = (TextBox)e.Row.FindControl("tbpayDate");
              tbpayDate.Attributes.Add("onClick", "new WdatePicker()");
              tbpayDate.Text = DateTime.Today.ToShortDateString ();

            }

        }
    

      
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string a = tbsheetID.Text;//要删除或者更新，先删除数据库里的数据，然后再保存
            PayManage bll = new PayManage();
            bll.Delete(int.Parse(a), 1);




            //下面是保存gv的数据
            int cid;
            string cName;
            cName = lblcID.Text.Trim();
            CustomsSystem.Model.Customer cus = new CustomsSystem.BLL.CustomerManage().GetModel(cName);
            cid = cus.ID;
            CustomsSystem.Model.Pay pay = new CustomsSystem.Model.Pay();
            CustomsSystem.BLL.BillManage bill = new BillManage();
            CustomsSystem.Model.Bill billmodel = new CustomsSystem.Model.Bill();
            for (int i = 0; i < gvActualReceivables.Rows.Count; i++)
            {
                pay.sheetID = int.Parse(tbsheetID.Text.Trim ());
                pay.cID =cid;
                pay.payTypeID = 1;
                pay.billID=int.Parse (((HiddenField )gvActualReceivables .Rows [i].FindControl ("billID")).Value );
                pay.feeTypeID = ((DropDownList)gvActualReceivables.Rows [i].FindControl("ddlfeeTypeID")).SelectedValue;
                pay.sfeeAmount = decimal.Parse(((TextBox)gvActualReceivables.Rows[i].FindControl("tbsfeeAmount")).Text);
                if (((TextBox)gvActualReceivables.Rows[i].FindControl("tbafeeAmount")).Text.Trim() == "")
                { pay.afeeAmount = 0; }//如果金额为空则默认0
                else
                { pay.afeeAmount = decimal.Parse(((TextBox)gvActualReceivables.Rows[i].FindControl("tbafeeAmount")).Text.Trim()); }
                pay.empID = ((DropDownList)gvActualReceivables.Rows[i].FindControl("ddlempID")).SelectedValue;
                pay.payType =int.Parse ( ((DropDownList)gvActualReceivables.Rows[i].FindControl("ddlpayType")).SelectedValue);
                pay.payBank = tbpayBank.Text.Trim();
                if (((TextBox)gvActualReceivables.Rows[i].FindControl("tbpayDate")).Text.Trim() == "")
                { pay.payDate = DateTime.Now; }//如果输入日期为空则为今天
                else
                { pay.payDate = DateTime.Parse(((TextBox)gvActualReceivables.Rows[i].FindControl("tbpayDate")).Text.Trim()); }

                pay.remark = ((TextBox)gvActualReceivables.Rows[i].FindControl("tbremark")).Text.Trim().ToString();
                pay.inputDate = DateTime.Now;
               
                bll.Add(pay);
                billmodel.billID = pay.billID;
                billmodel.afeeAmount = pay.afeeAmount;
                if (pay.afeeAmount >= pay.sfeeAmount)
                {
                    billmodel.payedID = 1;
                }
                else if (pay.afeeAmount < pay.sfeeAmount && pay.afeeAmount > 0)
                {
                    billmodel.payedID = 2;
                }
                else { billmodel.payedID = 0; }
                bill.UpdatePayedID(billmodel);
            }
            Response.Write("<script>alert('您已成功保存！');window.history.go(-2);</script>");
        }

   

        /// <summary>
        /// 获取费用类别
        /// </summary>
        /// <returns></returns>
        public DataTable GetFeeType()
        {
            FeeTypeManage feeTypeManage = new FeeTypeManage();
            DataSet ds = feeTypeManage.GetFeeList("");
            DataTable dt = ds.Tables[0];
            return dt;

        }
        /// <summary>
        /// 获取人员表
        /// </summary>
        /// <returns></returns>
        public DataTable GetEmpNum()
        {
            CustomsSystem.BLL.UserManage UserManage = new UserManage();
            DataSet ds = UserManage.GetAllList();
            DataTable dt = ds.Tables[0];
            return dt;

        }

        #region  全选
        /// <summary>
        /// 全选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlpayType_SelectedIndexChanged1(object sender, EventArgs e)
        {
            for (int i = 0; i < gvActualReceivables.Rows.Count; i++)
            {
                ((DropDownList)gvActualReceivables.Rows[i].FindControl("ddlpayType")).SelectedIndex = ddlpayType.SelectedIndex;
            }
        }

        protected void tbpayDate_TextChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < gvActualReceivables.Rows.Count; i++)
            {
                ((TextBox)gvActualReceivables.Rows[i].FindControl("tbpayDate")).Text = tbpayDate.Text.Trim();

            }
        }

        protected void tbpayBank_TextChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < gvActualReceivables.Rows.Count; i++)
            {
                ((TextBox)gvActualReceivables.Rows[i].FindControl("tbBank")).Text = tbpayBank.Text.Trim();

            }
        }

        protected void gvActualReceivables_DataBinding(object sender, EventArgs e)
        {
            for (int i = 0; i < gvActualReceivables.Rows.Count; i++)
            {
                if (decimal.Parse(((TextBox)gvActualReceivables.Rows[i].FindControl("afeeAmount")).Text) == 0)
                {
                    ((TextBox)gvActualReceivables.Rows[i].FindControl("afeeAmount")).Text = "";
                }

            }
        }

        protected void ddlempID_SelectedIndexChanged(object sender, EventArgs e)
        {

            for (int i = 0; i < gvActualReceivables.Rows.Count; i++)
            {
                ((DropDownList)gvActualReceivables.Rows[i].FindControl("ddlempID")).SelectedIndex = ddlempID .SelectedIndex ;
            }
        }
        #endregion
    }
}